package keensky.loginandupload;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import java.io.File;

public class MyUpload {

	public static void main(String args[]) {
		// 指定URL
		String targetURL = null;
		//  指定上传文件
		File targetFile = null;
		
		String path="C:\\Users\\lc\\Desktop";
		String fileFileName="tt.txt";

		targetFile = new File(path,fileFileName);
		
		targetURL ="http://localhost:8080/strutslogin/webRoot/upload";
//		targetURL ="http://localhost:8080/httpclienttest/fileupload";
		
		PostMethod postMethod = new PostMethod(targetURL);

		try {

			
			Part[] parts = { new FilePart(targetFile.getName(), targetFile) };
			postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
			
			System.out.println(postMethod.getRequestEntity());
			
			HttpClient client = new HttpClient();
			//配置最长连接超时
			client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
			int status = client.executeMethod(postMethod);
			//sc_ok=200
			if (status == HttpStatus.SC_OK) {
				System.out.println("上传成功");
				// 上传成功
			} else {
				System.out.println("上传失败");
				// 上传失败
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			postMethod.releaseConnection();
		}
	}
}